package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.LunarUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class LunarTime {
    private int ganIndex;
    private int zhiIndex;
    private Lunar lunar;

    public LunarTime(int lunarYear, int lunarMonth, int lunarDay,int hour,int minute,int second) {
        this.lunar = Lunar.fromYmdHms(lunarYear, lunarMonth, lunarDay, hour, minute, second);
        this.zhiIndex = LunarUtil.getTimeZhiIndex(String.format("%02d:%02d", hour, minute));
        this.ganIndex = (this.lunar.getDayGanIndexExact() % 5 * 2 + this.zhiIndex) % 10;
        
    }

    public static LunarTime fromYmdHms(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second)
    {
        return new LunarTime(lunarYear, lunarMonth, lunarDay, hour, minute, second);
    }

    public String getShengXiao()
    {
        return LunarUtil.SHENG_XIAO.get(this.zhiIndex + 1);
    }

    public String getZhi()
    {
        return LunarUtil.ZHI.get(this.zhiIndex + 1);
    }

    public String getGan()
    {
        return LunarUtil.GAN.get(this.ganIndex + 1);
    }

    public String getGanZhi()
    {
        return this.getGan() + this.getZhi();
    }

    public String getPositionXi()
    {
        return LunarUtil.POSITION_XI.get(this.ganIndex + 1);
    }

    public String getPositionXiDesc()
    {
        return LunarUtil.POSITION_DESC.get(this.getPositionXi());
    }

    public String getPositionYangGui()
    {
        return LunarUtil.POSITION_YANG_GUI.get(this.ganIndex + 1);
    }

    public String getPositionYangGuiDesc()
    {
        return LunarUtil.POSITION_DESC.get(this.getPositionYangGui());
    }

    public String getPositionYinGui()
    {
        return LunarUtil.POSITION_YIN_GUI.get(this.ganIndex + 1);
    }

    public String getPositionYinGuiDesc()
    {
        return LunarUtil.POSITION_DESC.get(this.getPositionYinGui());
    }

    public String getPositionFu()
    {
        return this.getPositionFuBySect(2);
    }

    public String getPositionFuBySect(int sect)
    {
        List<String> fu = 1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2;
        return fu.get(this.ganIndex + 1);
    }

    public String getPositionFuDesc()
    {
        return this.getPositionFuDescBySect(2);
    }

    public String getPositionFuDescBySect(int sect)
    {
        return LunarUtil.POSITION_DESC.get(this.getPositionFuBySect(sect));
    }

    public String getPositionCai()
    {
        return LunarUtil.POSITION_CAI.get(this.ganIndex + 1);
    }

    public String getPositionCaiDesc()
    {
        return LunarUtil.POSITION_DESC.get(this.getPositionCai());
    }

    public String getChong()
    {
        return LunarUtil.CHONG.get(this.zhiIndex);
    }

    public String getChongGan()
    {
        return LunarUtil.CHONG_GAN.get(this.ganIndex);
    }

    public String getChongGanTie()
    {
        return LunarUtil.CHONG_GAN_TIE.get(this.ganIndex);
    }

    public String getChongShengXiao()
    {
        String chong = this.getChong();
        for (int i = 0, j = LunarUtil.ZHI.size(); i < j; i++) {
            if(LunarUtil.ZHI.get(i).compareTo(chong) == 0){
                return LunarUtil.SHENG_XIAO.get(i);
            }
        }
        return "";
    }

    public String getChongDesc()
    {
        return "(" + this.getChongGan() + this.getChong() + ")" + this.getChongShengXiao();
    }

    public String getSha()
    {
        return LunarUtil.SHA.get(this.getZhi());
    }

    public String getNaYin()
    {
        return LunarUtil.NAYIN.get(this.getGanZhi());
    }

    public String getTianShen()
    {
        return LunarUtil.TIAN_SHEN.get((this.zhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(this.lunar.getDayZhiExact())) % 12 + 1);
    }

    public String getTianShenType()
    {
        return LunarUtil.TIAN_SHEN_TYPE.get(this.getTianShen());
    }

    public String getTianShenLuck()
    {
        return LunarUtil.TIAN_SHEN_TYPE_LUCK.get(this.getTianShenType());
    }

    public ArrayList<String> getYi()
    {
        return LunarUtil.getTimeYi(this.lunar.getDayInGanZhiExact(), this.getGanZhi());
    }

    public ArrayList<String> getJi()
    {
        return LunarUtil.getTimeJi(this.lunar.getDayInGanZhiExact(), this.getGanZhi());
    }

    public NineStar getNineStar()
    {
        String solarYmd = this.lunar.getSolar().toYmd();
        LinkedHashMap<String, Solar> jieQi = this.lunar.getJieQiTable();
        boolean asc = false;
        if (solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0) {
            asc = true;
        }
        int start = asc ? 7 : 3;
        String dayZhi = this.lunar.getDayZhi();
        if ("子午卯酉".contains(dayZhi)) {
            start = asc ? 1 : 9;
        } else if ("辰戌丑未".contains( dayZhi)) {
            start = asc ? 4 : 6;
        }
        int index = asc ? start + this.zhiIndex - 1 : start - this.zhiIndex - 1;
        if (index > 8) {
            index -= 9;
        }
        if (index < 0) {
            index += 9;
        }
        return new NineStar(index);
    }

    public int getGanIndex()
    {
        return this.ganIndex;
    }

    public int getZhiIndex()
    {
        return this.zhiIndex;
    }

    public String toString()
    {
        return this.getGanZhi();
    }


    public String getXun()
    {
        return LunarUtil.getXun(this.getGanZhi());
    }

    public String getXunKong()
    {
        return LunarUtil.getXunKong(this.getGanZhi());
    }

    public String getMinHm()
    {
        int hour = this.lunar.getHour();
        if (hour < 1) {
            return "00:00";
        } else if (hour > 22) {
            return "23:00";
        }
        return String.format("%02d:00", hour % 2 == 0 ? hour - 1 : hour);
    }

    public String getMaxHm()
    {
        int hour = this.lunar.getHour();
        if (hour < 1) {
            return "00:59";
        } else if (hour > 22) {
            return "23:59";
        }
        return String.format("%02d:59", hour % 2 == 0 ? hour : hour + 1);
    }

    public static void main(String[] args) {
        System.out.println("LunarTime");
    }
}
